package fr.ginvest.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "club", uniqueConstraints={ @UniqueConstraint (columnNames={"nom"})})
public class Club implements Serializable {

	private static final long serialVersionUID = -6664014620246082098L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO) 
	private int id;
	
	@Basic(optional=false)
	private String nom;
	
	@Column(length=5000)
	private String description;
	
	@OneToOne
	@JoinColumn(name="president_fk",referencedColumnName="id")
	private Angel president;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="Angel_Club",uniqueConstraints={ @UniqueConstraint (columnNames={"club_id","angel_id"})},
			inverseJoinColumns={@JoinColumn(name="angel_id",referencedColumnName="id")},
			joinColumns={@JoinColumn(name="club_id",referencedColumnName="id")})
	private Set<Angel> membres = new HashSet<Angel>();
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="Club_Ami",
			inverseJoinColumns={@JoinColumn(name="amis_id")},
			joinColumns={@JoinColumn(name="club_id")})
	private Set<Club> amis = new HashSet<Club>();
	
	@OneToMany(mappedBy="destination")
	private Collection<DemandeAngelClub> demandesAngel = new HashSet<DemandeAngelClub>();
	
	@OneToMany(mappedBy="destination")
	private Collection<DemandeClubAmi> demandesAmi = new HashSet<DemandeClubAmi>();
	
	@OneToMany(mappedBy="club")
	private Collection<Avancement> avancements = new HashSet<Avancement>();
	
	public Club() {}

	public Club(String nom, String description, Angel president) {
		super();
		this.nom = nom;
		this.description = description;
		this.president = president;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Club other = (Club) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Angel getPresident() {
		return president;
	}

	public void setPresident(Angel president) {
		this.president = president;
	}

	public Set<Angel> getMembres() {
		return membres;
	}

	public void setMembres(Set<Angel> membres) {
		this.membres = membres;
	}
	
	public void addMembre(Angel angel) {
		this.membres.add(angel);
	}

	public Set<Club> getAmis() {
		return amis;
	}

	public void setAmis(Set<Club> amis) {
		this.amis = amis;
	}
	
	public void addAmi(Club club) {
		// this.amis.add(club);
		if (!this.amis.contains(club)) {
			this.amis.add(club);
		}
		if (!club.getAmis().contains(this)) {
			club.getAmis().add(this);
		}
	}
	
	public boolean estAmiDe(Club c) {
		return this.amis.contains(c);
	}

	public Collection<DemandeAngelClub> getDemandesAngel() {
		return demandesAngel;
	}

	public Collection<DemandeClubAmi> getDemandesAmi() {
		return demandesAmi;
	}

	public Collection<Avancement> getAvancements() {
		return avancements;
	}

	public void setAvancements(Collection<Avancement> avancements) {
		this.avancements = avancements;
	}
	
	public void addAvancement(Avancement avancement) {
		this.avancements.add(avancement);
	}
	
}
